
//
//  DingdanDetailViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 16/3/11.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "DingdanDetailViewController.h"

#import "DataProvider.h"
#import "SVProgressHUD.h"

#import "Dingdan_Model.h"
#import "DingdanList_Model.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
@interface DingdanDetailViewController () <UITableViewDataSource, UITableViewDelegate>
//数据
@property (nonatomic, strong) NSMutableArray * arr_all;
@property (nonatomic, strong) NSMutableArray * arr_list;

@property (nonatomic, strong) UITableView * tableView;

@end

@implementation DingdanDetailViewController
{
    BOOL issend;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    issend=NO;
    [self p_data];
    
    [self p_navigationController];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"订单详情";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}

//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 12 + self.arr_list.count;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger num = self.arr_list.count;
    
    if(indexPath.row == 0)
    {
        return 130;
    }
    else if(indexPath.row == 1 )
    {
        return 40;
    }
    else if (indexPath.row >(num+1)&&indexPath.row<=num+13)
    {
        return 50;
    }
    else
    {
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger num = self.arr_list.count;
    
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    if(indexPath.row == 0)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor orangeColor];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH - 30, 30)];
        name.text = @"联系人: wolaji";
        name.textColor = [UIColor whiteColor];
        [cell addSubview:name];
        
        UILabel * tel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(name.frame) + 7, SCREEN_WIDTH - 30, 30)];
        tel.text = @"联系人电话: 1999999999";
        tel.textColor = [UIColor whiteColor];
        [cell addSubview:tel];
        
        
        UILabel * address = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(tel.frame) + 7, SCREEN_WIDTH - 30, 30)];
        address.text = @"联系人地址: 就是看的就是空间打开手机打开";
        address.textColor = [UIColor whiteColor];
        [cell addSubview:address];
        
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];
        
        UIView * view_line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 125, SCREEN_WIDTH, 5)];
        view_line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line1];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            
            name.text = [NSString stringWithFormat:@"联系人: %@",model.linkname];
            tel.text = [NSString stringWithFormat:@"联系人电话: %@",model.linkmobile];
            address.text = [NSString stringWithFormat:@"联系人地址: %@",model.linkaddress];
        }
        
    }
    else if (indexPath.row == 1)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH / 2 + 50, 30)];
        name.text = @"订单号";
        
        [cell addSubview:name];
        
        UILabel * type = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 5, 5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 10, 30)];
        type.text = @"未付款";
        type.textColor = [UIColor orangeColor];
        type.textAlignment = NSTextAlignmentRight;
        [cell addSubview:type];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            
            name.text = model.ordernum;
            
            if([model.type integerValue] == 0)
            {
                switch ([model.state integerValue])
                {
                    case 0:
                    {
                        type.text = @"未付款";
                    }
                        break;
                    case 1:
                    {
                        type.text = @"未消费";
                    }
                        break;
                    case 2:
                    {
                        type.text = @"已完成";
                    }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch ([model.state integerValue])
                {
                    case 0:
                    {
                        type.text = @"未付款";
                    }
                        break;
                    case 1:
                    {
                        type.text = @"已付款";
                    }
                        break;
                    case 2:
                    {
                        type.text = @"已收货";
                    }
                        break;
                    default:
                        break;
                }
            }
        }

    }
    else if(indexPath.row == num + 2)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"xian124@2x"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"订单号";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = model.ordernum;
        }
    }
    else if(indexPath.row == num + 3)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"xian126@2x"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"消费日期";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"%@",model.datetime];
        }
    }
    else if(indexPath.row == num + 4)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"xian12@2x"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"消费总额";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"￥ %@",model.price];
        }
    }
    else if(indexPath.row == num + 5)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"youhuiquan"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"优惠券";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        //        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"优惠￥%@",model.cprice];
        }
    }
    else if(indexPath.row == num + 6)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"diyongbi"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"抵用币";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        //        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"%@",model.diyongbi];
        }
    }
    else if(indexPath.row == num + 7)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"guanzhubi"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"关注币";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        //        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"%@",model.guanzhubi];
        }
    }
    else if(indexPath.row == num + 8)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"xian123@2x"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"积分";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        //        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"%@",model.score];
        }
    }
    else if(indexPath.row == num + 9)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:issend?@"peisongfei":@"renshu"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = issend?@"配送费":@"到店人数";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
        //        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"%@",issend?model.postage:model.pnum];
        }
    }
    else if(indexPath.row == num + 10)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
        image.image = [UIImage imageNamed:@"baozhuangfe"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
        name.text = @"包装费";
        [cell addSubview:name];
        
        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
        detail.text = @"12323123";
        detail.textAlignment = NSTextAlignmentRight;
//        detail.textColor = [UIColor orangeColor];
        [cell addSubview:detail];
        
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            detail.text = [NSString stringWithFormat:@"%@",model.otherfee];
        }
    }
//    else if(indexPath.row == num + 11)
//    {
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        
//        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
//        image.image = [UIImage imageNamed:@"xian125@2x"];
//        [cell addSubview:image];
//        
//        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 12.5, 100, 25)];
//        name.text = @"实际支付";
//        [cell addSubview:name];
//        
//        UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(name.frame) - 20, 25)];
//        detail.text = @"12323123";
//        detail.textAlignment = NSTextAlignmentRight;
//        detail.textColor = [UIColor orangeColor];
//        [cell addSubview:detail];
//        
//        if(self.arr_all.count != 0)
//        {
//            Dingdan_Model * model = self.arr_all[indexPath.section];
//            
//            float price = [model.price floatValue] - [model.arrivescore floatValue];
//            
//            detail.text = [NSString stringWithFormat:@"￥ %.2f",price];
//        }
//    }
    else if(indexPath.row == num + 11)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn.frame = CGRectMake(SCREEN_WIDTH - 90, 5, 80, 40);
        btn.tag = 10000 + indexPath.section;
        btn.backgroundColor = [UIColor orangeColor];
        [btn setTitle:@"订单" forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        
        [btn addTarget:self action:@selector(btnActionDingdan:) forControlEvents:(UIControlEventTouchUpInside)];
        
        UIButton * btn1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn1.frame = CGRectMake(SCREEN_WIDTH - 90 - 90, 5, 80, 40);
        btn1.tag = 20000 + indexPath.section;
        btn1.backgroundColor = [UIColor orangeColor];
        [btn1 setTitle:@"订单" forState:(UIControlStateNormal)];
        [btn1 setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        
        [btn1 addTarget:self action:@selector(btn1ActionDingdan:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        [cell addSubview:btn];
        [cell addSubview:btn1];
        if(self.arr_all.count != 0)
        {
            Dingdan_Model * model = self.arr_all[indexPath.section];
            
            if([model.type integerValue] == 0)
            {
                switch ([model.state integerValue])
                {
                    case 0:
                    {
                        //                        [btn setTitle:@"取消订单" forState:(UIControlStateNormal)];
                        btn.hidden = YES;
                    }
                        break;
                    case 1:
                    {
                        [btn setTitle:@"确认消费" forState:(UIControlStateNormal)];
                        [btn1 setTitle:@"取消订单" forState:(UIControlStateNormal)];

                    }
                        break;
                    case 2:
                    {
                        //                        [btn setTitle:@"待评价" forState:(UIControlStateNormal)];
                        btn.hidden = YES;
                        
                    }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch ([model.state integerValue])
                {
                    case 0:
                    {
                        //                        [btn setTitle:@"取消订单" forState:(UIControlStateNormal)];
                        btn.hidden = YES;
                    }
                        break;
                    case 1:
                    {
                        if([model.delivertytime length] == 0)
                        {
                            [btn setTitle:@"发货" forState:(UIControlStateNormal)];
                            [btn1 setTitle:@"取消订单" forState:(UIControlStateNormal)];
                        }
                        else
                        {
                            [btn setTitle:@"已发货" forState:(UIControlStateNormal)];
                            [btn1 setTitle:@"取消订单" forState:(UIControlStateNormal)];
                        }

                    }
                        break;
                    case 2:
                    {
                        btn.hidden = YES;
                        //                        [btn setTitle:@"删除订单" forState:(UIControlStateNormal)];
                    }
                        break;
                    default:
                        break;
                }
            }
        }
    }
    else
    {
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
        image.image = [UIImage imageNamed:@"1136-1"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 10, SCREEN_WIDTH - CGRectGetMaxX(image.frame) - 10 - 70, 30)];
        name.text = @"哈哈哈哈哈哈哈哈哈哈哈哈";
        name.font = [UIFont systemFontOfSize:15];
        [cell addSubview:name];
        
        UILabel * price = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 5, 10, 55, 30)];
        price.text = @"￥200.00";
        price.textAlignment = NSTextAlignmentRight;
        price.font = [UIFont systemFontOfSize:13];
        [cell addSubview:price];
        
        UILabel * num = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame) + 5, CGRectGetMaxY(price.frame), 55, 30)];
        num.text = @"x2";
        num.textAlignment = NSTextAlignmentRight;
        num.font = [UIFont systemFontOfSize:13];
        num.textColor = [UIColor grayColor];
        [cell addSubview:num];
        
        if(self.arr_list.count != 0)
        {
            DingdanList_Model * model = self.arr_list[indexPath.row - 2];
            NSString *url = [NSString stringWithFormat:@"%@upload/%@",Url,model.imgpath];
            [image sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"1136-1"]];
            
            name.text = model.goodsName;
            
            price.text = [NSString stringWithFormat:@"￥ %@",model.price];
            
            num.text = [NSString stringWithFormat:@"x%@",model.quantity];
        }
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 点击事件
- (void )btnActionDingdan:(UIButton *)sender
{
    NSInteger index = sender.tag - 10000;
    //    NSLog(@"%ld",(long)index);
    Dingdan_Model * model = self.arr_all[index];
    
    if([[NSString stringWithFormat:@"%@",model.type] isEqualToString:@"0"])
    {//确认订单
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请输入订单验证码" message:@"提示" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"请输入订单确认码";
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            //                textField.layer.borderColor = [UIColor grayColor].CGColor;
            //                textField.layer.borderWidth = 1;
        }];
        
        UIAlertAction * action_ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self.view endEditing:YES];
            
            UITextField * textField = alert.textFields.firstObject;
            NSLog(@"%@",textField.text);
            
            DataProvider * dataprovider = [[DataProvider alloc] init];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            
            [dataprovider setDelegateObject:self setBackFunctionName:@"identifycode:"];
            
            [dataprovider identifycodeWithcode:textField.text merchantid:[defaults objectForKey:@"merchantid"]];
        }];
        
        UIAlertAction * action_cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self.view endEditing:YES];
            
        }];
        
        [alert addAction:action_cancel];
        [alert addAction:action_ok];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
    else
    {//发货
        if([model.delivertytime length] == 0)
        {
            DataProvider * dataprovider = [[DataProvider alloc] init];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            
            [dataprovider setDelegateObject:self setBackFunctionName:@"mconfirmorder:"];
            
            [dataprovider mconfirmorderWithOrderid:model.orderid opera:@"1" merchanid:[defaults objectForKey:@"merchantid"]];
        }
        else
        {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经发货" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
            UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:action];
            
        }
    }
}

- (void )btn1ActionDingdan:(UIButton *)sender
{
    NSInteger index = sender.tag - 20000;
    Dingdan_Model * model = self.arr_all[index];

    if([[NSString stringWithFormat:@"%@",model.type] isEqualToString:@"0"])
    {
        DataProvider * dataprovider = [[DataProvider alloc] init];
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        
        [dataprovider setDelegateObject:self setBackFunctionName:@"mconfirmorder:"];
        
        [dataprovider mconfirmorderWithOrderid:model.orderid opera:@"2" merchanid:[defaults objectForKey:@"merchantid"]];
    }
    else
    {
        if([model.delivertytime length] == 0)
        {
            DataProvider * dataprovider = [[DataProvider alloc] init];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            
            [dataprovider setDelegateObject:self setBackFunctionName:@"mconfirmorder:"];
            
            [dataprovider mconfirmorderWithOrderid:model.orderid opera:@"2" merchanid:[defaults objectForKey:@"merchantid"]];
        }
        else
        {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经发货,不能取消订单" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
            UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:action];
        }
    }
    
    
}

//
- (void)mconfirmorder:(id )dict
{
    //    NSLog(@"%@",dict);
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            [SVProgressHUD showSuccessWithStatus:@"取消成功"];
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}

//根据确认码确认消费
- (void)identifycode:(id )dict
{
    //    NSLog(@"%@",dict);
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            [SVProgressHUD showSuccessWithStatus:@"消费成功" maskType:(SVProgressHUDMaskTypeBlack)];
            
            [self p_data];
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新tableView(记住,要更新放在主线程中)
                
                [self.tableView reloadData];
            });
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}



#pragma mark - 数据
- (void)p_data
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    
    [dataprovider setDelegateObject:self setBackFunctionName:@"orderdetail:"];
    
    [dataprovider orderdetailWithOrderid:self.orderid];
}

- (void)orderdetail:(id )dict
{
//    NSLog(@"%@",dict);
    
    self.arr_all = nil;
    self.arr_list = nil;
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            NSDictionary * dic = dict[@"data"];
            
            Dingdan_Model * model = [[Dingdan_Model alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arr_all addObject:model];
            
            for (NSDictionary * dict_list in dic[@"goodlist"])
            {
                DingdanList_Model * model = [[DingdanList_Model alloc] init];
                
                [model setValuesForKeysWithDictionary:dict_list];
                
                [self.arr_list addObject:model];
            }
            if ([dict[@"data"][@"type"] integerValue]==1) {
                issend=YES;
            }
            
            
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新tableView(记住,要更新放在主线程中)
                
                [self.tableView reloadData];
            });
        }
    }
    else
    {
//        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_all
{
    if(_arr_all == nil)
    {
        self.arr_all = [NSMutableArray array];
    }
    
    return _arr_all;
}

- (NSMutableArray *)arr_list
{
    if(_arr_list == nil)
    {
        self.arr_list = [NSMutableArray array];
    }
    
    return _arr_list;
}



@end
